package com.lagou.plugin;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;


import java.sql.Connection;
import java.util.Properties;

/**
 * @program: mybatis_multitable
 * @description: 自定义
 * @author: wyw
 * @create: 2020-01-17 18:21
 */
@Intercepts({
        @Signature(
                type = StatementHandler.class,
                method = "prepare",
                args = {
                        Connection.class,Integer.class
                }
        )
})
public class MyPlugin implements Interceptor {
    /**
     * 拦截方法：只要被拦截的目标对象的方法执行时，每次都会执行intercept方法
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("对方法进行了增强...");
        //原方法执行
        return invocation.proceed();
    }

    /**
     * 将当前拦截器生成代理存入拦截器链中
     * @param target 被拦截的目标对象
     * @return
     */
    @Override
    public Object plugin(Object target) {
        Object wrap = Plugin.wrap(target, this);
        return wrap;
    }

    /**
     * 获取配置文件的参数
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        System.out.println("获取到的配置文件的参数是： " + properties);
    }
}
